
import throttle from '../../../utils/throttle'
import {
  playStore,
  audioContext
} from '../../../store/playState'

const app = getApp()
const playModeNames = ['order', 'repeat', 'random']

Page({
  data: {
    id: 0,
    currSong: {},
    lyricInfo: '',
    currTime: 0,
    durationTime: 0,
    isPlay: true,
    lyricInfos: [],
    currentLyricIndex: 0,
    playSongs: [],
    songIndex: 0,
    playModeName: 'order',

    statusHeight: 20,
    contentHeight: 0,
    currIndex: 0,
    tabsItem: ['歌曲', '歌词'],
    processValue: 0,
    isSlider: false,
    scrollTop: 0,
    isFirstPlay: true,
    stateKeys: ['id', 'currSong', 'lyricInfo', 'lyricInfos', 'durationTime', 'currTime', 'currentLyricIndex','isPlay','playModeIndex']
  },
  onLoad(options) {
    // 获取设备信息
    // 轮播图高度
    this.setData({
      contentHeight: app.globalData.contentHeight
    })
    // 状态栏高度
    this.setData({
      statusHeight: app.globalData.statusHeight
    })
    // 获取id
    const id = options.id
    // 如果有传过来的id   才去切换到对应的歌曲
    if(id) {
      playStore.setState('isPlay',true)
      playStore.dispatch('changePlayMusicWithId', id)
    }
    // 获取state中的共享数据
    playStore.onStates(['playSongs', 'songIndex'], this.getChangeValue)
    playStore.onStates(this.data.stateKeys,this.getPlayerHandler)
  },
  updateTime: throttle(function(currTime) {
    if(this.data.isSlider) return
    const sliderValue = currTime / this.data.durationTime * 100
    this.setData({
      currTime: currTime,
      processValue: sliderValue
    })
  }, 800, {
    leading: false,
    trailing: false
  }),

  // 滚动轮播图后改变导航栏活跃状态
  onCurrSlider(event) {
    this.setData({
      currIndex: event.detail.current
    })
  },
  // 点击导航栏选项
  onClickTab(event) {
    const index = event.currentTarget.dataset.index
    this.setData({
      currIndex: index
    })
  },
  // 点击进度条
  onSliderChange(event) {
    const value = event.detail.value
    // this.data.processValue = this.data.currTime / this.data.durationTime * 100
    // processValue => value
    const currTime = value / 100 * this.data.durationTime
    audioContext.seek(currTime / 1000)
    this.setData({
      currTime,
      isSlider: false,
      processValue: value
    })
  },
  // 滑动滑块
  // onSliderChanging: function() {}
  onSliderChanging: throttle(function(event) {
    const value = event.detail.value
    const currTime = value / 100 * this.data.durationTime
    this.setData({
      currTime,
      isSlider: true
    })
  },100),
  // 点击 播放  / 暂停
  onPlayChange() {
    playStore.dispatch('changePlayMusicWithIdAction')
  },
  // 点击返回
  onBackLeft() {
    wx.navigateBack()
  },
  getChangeValue({
    playSongs,
    songIndex
  }) {
    if (playSongs) {
      this.setData({
        playSongs
      })
    }
    if (songIndex !== undefined) {
      this.setData({
        songIndex
      })
    }
  },
  getPlayerHandler({
    id,
    currTime,
    durationTime,
    lyricInfo,
    lyricInfos,
    currentLyricIndex,
    currSong,
    isPlay,
    playModeIndex
  }) {
    if (id !== undefined) {
      this.setData({
        id
      })
    }
    if (currTime !== undefined) {
      this.updateTime(currTime)
    }
    if (durationTime !== undefined) {
      this.setData({
        durationTime
      })
    }
    if (lyricInfo !== undefined) {
      this.setData({
        lyricInfo
      })
    }
    if (lyricInfos !== undefined) {
      this.setData({
        lyricInfos
      })
    }
    if (currSong !== undefined) {
      this.setData({
        currSong
      })
    }
    if (currentLyricIndex !== undefined) {
      this.setData({
        currentLyricIndex,
        scrollTop: currentLyricIndex * 36
      })
    }
    if(isPlay !== undefined) {
      this.setData({ isPlay })
    }
    if(playModeIndex !== undefined) {
      this.setData({
        playModeName: playModeNames[playModeIndex]
      })
    }
  },
  // 下一首
  onPrevTap() {
    playStore.dispatch('changePlayNewSong',false)
  },
  // 上一首
  onNextTap() {
    playStore.dispatch('changePlayNewSong')
  },
  // 点击播放模式
  onPlayMode() {
    playStore.dispatch('changePlayModeIndex')
  },
  onUnload() {
    playStore.offStates(['playSongs', 'songIndex'], this.getChangeValue)
    playStore.offStates(this.data.stateKeys,this.getPlayerHandler)
  }
})